/**
 * Created by sens on 2020/5/4.
 */
(function () {
    typeof window.sens !== 'object' && (window.sens = {});
})(window);

Object.assign(window.sens, (function () {
    let maxProp = 1000;

    function calcMiss(_my, _other, aglFix, aglEff) {
        if (_other.agl < _my.agl * aglFix) {
            return false;
        }

        let randMiss = Math.floor(Math.random() * 200);
        let miss = (_other.aglStore - _my.aglStore < 0 ? 0 : _other.aglStore - _my.aglStore);
        let isMiss = false;
        let addAgl = _other.agl - _my.agl * aglFix;

        if (randMiss * aglFix < miss) {
            isMiss = true;

            if (_other.agl < _my.agl) {
                _other.aglStore /= 2;
            }
        } else {
            _other.aglStore += addAgl * aglEff / aglFix;
        }

        return isMiss;
    }

    function huoqiu(_my, _other, is1P) {
        _my.skillStore -= 100;
        let aglFix = 1.2;
        let aglEff = 0.8;
        let isMiss = calcMiss(_my, _other, aglFix, aglEff);

        if (isMiss) {
            sens.log('[' + _my.name + ']突然高举双手，只见双手出现了一个火球，砸向[' + _other.name + '],但是没砸中！');
        } else {
            _other.def = _other.def - 10 < 0 ? 0 : _other.def - 10;
            let hurt = Math.floor(_my.int * (maxProp - _other.def + _my.int * 0.5) / maxProp) + Math.floor(Math.random() * 20) + 10;
            _other.HP -= hurt;
            _other.HP <= 0 && (_other.HP = 0);
            sens.getOption().onBloodChange(is1P, _other.HP > 0 ? _other.HP / _other.maxHp : 0);

            sens.log('[' + _my.name + ']突然高举双手，只见双手出现了一个火球，砸向[' + _other.name + '],[' + _other.name + ']衣服烧光了防御下降,受到了【' + hurt + '】点伤害![' + _other.name + ']剩余HP:' + _other.HP, is1P)

            _other.HP === 0 && sens.stop(_my, _other);
        }
    }

    function huolong(_my, _other, is1P) {
        _my.skillStore -= 200;
        let aglFix = 1.5;
        let aglEff = 0.8;
        let isMiss = calcMiss(_my, _other, aglFix, aglEff);

        if (isMiss) {
            sens.log('[' + _my.name + ']突然高举双手，只见火焰聚集，化作一条火龙冲向[' + _other.name + '],但是没砸中！');
        } else {
            _other.def = _other.def - 20 < 0 ? 0 : _other.def - 20;
            let hurt = Math.floor(_my.int * 1.5 * (maxProp - _other.def + _my.int * 0.5) / maxProp) + Math.floor(Math.random() * 40) + 20;
            _other.HP -= hurt;
            _other.HP <= 0 && (_other.HP = 0);
            sens.getOption().onBloodChange(is1P, _other.HP > 0 ? _other.HP / _other.maxHp : 0);

            sens.log('[' + _my.name + ']突然高举双手，只见火焰聚集，化作一条火龙冲向[' + _other.name + '],[' + _other.name + ']衣服烧光了防御下降,受到了【' + hurt + '】点伤害![' + _other.name + ']剩余HP:' + _other.HP, is1P)

            _other.HP === 0 && sens.stop(_my, _other);
        }
    }

    function lianji(_my, _other, is1P) {
        _my.skillStore -= 150;
        let attCount = 5;

        doLianji();
        function doLianji() {
            _my.skillStore -= 50;

            let aglFix = Math.pow(0.8, 6 - attCount);
            let aglEff = 0.8;
            let isMiss = calcMiss(_my, _other, aglFix, aglEff);

            if (attCount) {
                if (isMiss) {
                    sens.log('[' + _my.name + ']冲向[' + _other.name + ']发动了连续普通拳,但是扑了个空！');
                } else {
                    attCount--;
                    _other.def = _other.def - 10 < 0 ? 0 : _other.def - 10;
                    let hurt = Math.floor(_my.att * (maxProp - _other.def + _my.att * 0.5) / maxProp * 0.5) + Math.floor(Math.random() * 10);
                    _other.HP -= hurt;
                    _other.HP <= 0 && (_other.HP = 0);
                    sens.getOption().onBloodChange(is1P, _other.HP > 0 ? _other.HP / _other.maxHp : 0);

                    sens.log('[' + _my.name + ']冲向[' + _other.name + ']发动了连续普通拳,[' + _other.name + ']受到了【' + hurt + '】点伤害![' + _other.name + ']剩余HP:' + _other.HP, is1P)

                    if (_other.HP > 0) {
                        if (_my.skillStore > 0 ) {
                            doLianji();
                        } else {
                            sens.log('[' + _my.name + ']体力耗尽，实在挥不动无力的拳头了!');
                        }
                    } else {
                        sens.stop(_my, _other);
                    }
                }
            }
        }
    }

    function chongzhuang(_my, _other, is1P) {
        _my.skillStore -= 50;
        let aglFix = 1;
        let aglEff = 0.8;
        let isMiss = calcMiss(_my, _other, aglFix, aglEff);

        if (isMiss) {
            let hurt = Math.floor(_my.maxHp / 10);
            sens.log('[' + _my.name + ']像一只蛮牛疯狂的冲向[' + _other.name + '],但是扑了个空,摔倒了！' + '自己受到了【' + hurt + '】点伤害!', !is1P);
            _my.HP -= hurt;
            _my.HP <= 0 && (_other.HP = 0);
            sens.getOption().onBloodChange(!is1P, _my.HP > 0 ? _my.HP / _my.maxHp : 0);

            _my.HP === 0 && sens.stop(_other, _my);
        } else {
            let hurt = Math.floor((_my.def * 0.5 + _my.maxHp / 8) * (maxProp - _other.def + _my.def * 0.5) / maxProp) + Math.floor(Math.random() * 50);
            _other.HP -= hurt;
            _my.HP -= Math.floor(hurt / 2);
            _other.HP <= 0 && (_other.HP = 0);
            _my.HP <= 0 && (_my.HP = 0);
            sens.getOption().onBloodChange(is1P, _other.HP > 0 ? _other.HP / _other.maxHp : 0);
            sens.getOption().onBloodChange(!is1P, _my.HP > 0 ? _my.HP / _my.maxHp : 0);

            sens.log('[' + _my.name + ']像一只蛮牛疯狂的冲向[' + _other.name + '],[' + _other.name + ']被撞翻在地,受到了【' + hurt + '】点伤害![' + _other.name + ']剩余HP:' + _other.HP, is1P);
            sens.log('[' + _my.name + ']自己受到了【' + Math.floor(hurt / 2) + '】点伤害![' + _my.name + ']剩余HP:' + _my.HP, !is1P)

            _other.HP === 0 && sens.stop(_my, _other);
            _other.HP !== 0 && _my.HP === 0 && sens.stop(_other, _my);
        }
    }

    function zhiliao (_my, _other, is1P) {
        _my.skillStore -= 150;
        //增加生命
        let hp = parseInt(_my.int * 1.2 + Math.floor(Math.random() * 20));
        _my.HP += hp;
        _my.HP > _my.maxHp && (_my.HP = _my.maxHp);
        sens.getOption().onBloodChange(!is1P, _my.HP > 0 ? _my.HP / _my.maxHp : 0);

        sens.log('[' + _my.name + ']释放了小型治疗，为自己增加了' + hp + 'HP,剩余血量HP:' + _my.HP, !is1P);
    }

    function tiejia (_my, _other, is1P) {
        _my.skillStore -= 200;
        //增加防御
        _my.def = _my.def + _my.def / 10 > maxProp ? maxProp : _my.def + _my.def / 10;
        sens.log('[' + _my.name + ']释放了铁甲术，感觉自己变硬了！！');
    }

    let jopMap = [
        {
            name: '疯狂的',
            skills: [
                {
                    name: '火球术',
                    need: 100,
                    rate: 2,
                    start: huoqiu
                },
                {
                    name: '二连击',
                    need: 200,
                    rate: 8,
                    start: lianji
                }
            ]
        },
        {
            name: '玩火的',
            skills: [
                {
                    name: '火球术',
                    need: 100,
                    rate: 5,
                    start: huoqiu
                },
                {
                    name: '火龙术',
                    need: 200,
                    rate: 5,
                    start: huolong
                }
            ]
        },
        {
            name: '智障的',
            skills: [
                {
                    name: '冲撞',
                    need: 50,
                    rate: 10,
                    start: chongzhuang
                }
            ]
        },
        {
            name: '仁慈的',
            skills: [
                {
                    name: '治疗术',
                    need: 150,
                    rate: 7,
                    start: zhiliao
                },
                {
                    name: '铁甲术',
                    need: 200,
                    rate: 3,
                    start: tiejia
                }
            ]
        }
    ];

    return {
        jopMap: jopMap
    }
})());